import type { SogouElements } from '../../../../../../types/constants/personalization/sogou'
import type { ThemeType } from '../../../../../../types/interfaces'
import { SearchThemeService } from '../../../../../../lib/abstract'
import { PageHandler } from '../../../../../../lib/page_handler'
import { SogouPageType } from '../../../../../../lib/page_handler/platform/sogou/types'
import {
  UNDEFINED_PAGE_TYPE,
  UNDEFINED_PAGE_TYPE_TIPS,
} from '../../../../../../lib/page_handler/type'
import { BODY_SELECTOR, logger, loggerDeduplicator, PLATFORM_PAGE_TYPES, SUPPORTED_PLATFORMS } from '../../../../../../types/constants'
import { SogouNotFoundPage } from './sogou/page/404'
import { SogouNotFoundWeixinPage } from './sogou/page/404_weixin'
import { SogouAskPage } from './sogou/page/ask'
import { SogouCommonPage } from './sogou/page/common'
import { SogouChinesePage } from './sogou/page/deprecated/chinese'
import { SogouTranslationPage } from './sogou/page/deprecated/translation'
import { SogouEncyclopediaPage } from './sogou/page/encyclopedia'
import { SogouIndexEncyclopediaPage } from './sogou/page/index_encyclopedia'
import { SogouIndexKnowledgePage } from './sogou/page/index_knowlege'
import { SogouIndexSearchPage } from './sogou/page/index_search'
import { SogouIndexWeixinPage } from './sogou/page/index_weixin'
import { SogouKnowledgePage } from './sogou/page/knowledge'
import { SogouMedicalPage } from './sogou/page/medical'
import { SogouMorePage } from './sogou/page/more'
import { SogouNewsPage } from './sogou/page/news'
import { SogouVideoPage } from './sogou/page/video'
import { SogouWebPage } from './sogou/page/web'
import { SogouWeixinPage } from './sogou/page/weixin'
import { SogouZhihuPage } from './sogou/page/zhihu'

/**
 * 页面处理器
 */
const PAGE_HANDLERS: Partial<Record<SogouPageType, any>> = {
  [SogouPageType.IndexSearch]: SogouIndexSearchPage,
  [SogouPageType.IndexWeixin]: SogouIndexWeixinPage,
  [SogouPageType.IndexKnowledge]: SogouIndexKnowledgePage,
  [SogouPageType.IndexEncyclopedia]: SogouIndexEncyclopediaPage,
  [SogouPageType.App]: SogouAskPage,
  [SogouPageType.Ask]: SogouAskPage,
  [SogouPageType.Chinese]: SogouChinesePage,
  [SogouPageType.Encyclopedia]: SogouEncyclopediaPage,
  [SogouPageType.Knowledge]: SogouKnowledgePage,
  [SogouPageType.Medical]: SogouMedicalPage,
  [SogouPageType.News]: SogouNewsPage,
  [SogouPageType.Translation]: SogouTranslationPage,
  [SogouPageType.Video]: SogouVideoPage,
  [SogouPageType.Web]: SogouWebPage,
  [SogouPageType.Weixin]: SogouWeixinPage,
  [SogouPageType.Zhihu]: SogouZhihuPage,
  [SogouPageType.More]: SogouMorePage,
  [SogouPageType.NotFound]: SogouNotFoundPage,
  [SogouPageType.WeixinNotFound]: SogouNotFoundWeixinPage,
}

/**
 * 主题服务测试链接
 */
export class SogouThemeService extends SearchThemeService {
  readonly name: string = 'SogouTheme'
  readonly platformPageType: string = PLATFORM_PAGE_TYPES.SEARCH.value
  private static instance: SogouThemeService
  declare readonly elements: SogouElements
  private readonly commonPage: SogouCommonPage = SogouCommonPage.getInstance()
  private readonly pageHandler: PageHandler = PageHandler.getInstance()
  private currentPageType: SogouPageType | 'default' = this.pageHandler.pages.sogou.pageType
  readonly wrapperSelector: string = BODY_SELECTOR

  constructor() {
    super(SUPPORTED_PLATFORMS.SO_GOU)

    this.updateElementStylesForTheme()
    this.checkPageType(this.currentPageType)
  }

  /**
   * 获取单例实例
   */
  static getInstance(): SogouThemeService {
    if (!SogouThemeService.instance) {
      SogouThemeService.instance = new SogouThemeService()
    }
    return SogouThemeService.instance
  }

  /**
   * 批量初始化页面样式
   */
  updateElementStylesForTheme(): void {
    // 通用样式初始化
    this.commonPage.init()

    // 动态获取当前页面处理器并初始化
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as SogouPageType]
    if (PageHandlerClass) {
      PageHandlerClass.getInstance().init()
    }
    else if (this.currentPageType === UNDEFINED_PAGE_TYPE) {
      loggerDeduplicator.warn(UNDEFINED_PAGE_TYPE_TIPS)
    }
    else {
      logger.warn(`未找到 ${this.currentPageType} 页面的处理器`)
    }
  }

  platformStyleVar(_theme: ThemeType): string {
    return ``
  }

  getBaseStyles(_theme: ThemeType): string {
    return `
${super.getBaseStyles(_theme)}
`
  }

  getPageStyles(_theme: ThemeType): string {
    const supportPages = Object.values(SogouPageType)

    return !supportPages.includes(this.currentPageType as SogouPageType)
      ? ''
      : `
${BODY_SELECTOR} {
  background-color: var(${this.themeVarService.search.default.background}) !important;
  color: var(${this.themeVarService.search.default.text}) !important;
  overflow-x: hidden !important;
}
`
  }

  getMainStyles(theme: ThemeType): string {
    // 1. 通用样式
    let commonStyles = this.commonPage.getMainStyles()
    const notSupportedPages: SogouPageType[] = [
      SogouPageType.NotFound,
    ]

    if (notSupportedPages.includes(this.currentPageType as SogouPageType))
      commonStyles = ''

    // 2. 动态获取当前页面样式
    let pageStyles = ''
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as SogouPageType]
    if (PageHandlerClass) {
      pageStyles = PageHandlerClass.getInstance().getMainStyles(theme)
    }

    return `
  /* ===================== 【页面】通用样式 ===================== */   
  ${commonStyles}
  /* ===================== 【页面】独立样式 ===================== */
  ${pageStyles}
`
  }

  getPatchedStyles(_theme: ThemeType): string {
    return ``
  }

  getFixedStyles(_theme: ThemeType): string {
    return ``
  }
}
